<?php
/*
Copyright 2011 Saiyasodharan (http://saiy2k.blogspot.com/)

This file is part of SpiroCanvas (http://code.google.com/p/spirocanvas/)

SpiroCanvas is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

SpiroCanvas is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SpiroCanvas.  If not, see <http://www.gnu.org/licenses/>.
*/

require 'config.php';


$cl = curl_init($FLICKR_UPLOAD_URL);

echo('init');

$data   =       array(  "api_key" => $FLICKR_API_KEY,
                                        "auth_token" => $_POST['token']);

//get the base64 data from POST arguments
$b64    =       $_POST['data'];

//remove this "data:image/png;base64" header from the base64 data
$clean  =       substr($b64, 22);

//replace blanks(generated by toDataURL()) with pluses for base64_decode to work correct
$clean = str_replace(' ','+',$clean);

//decode the raw data and save it in a file
$FILE_PATH = $_POST['token'].'.png';
file_put_contents($FILE_PATH, base64_decode($clean));
                                        
$data['photo'] = '@' . realpath($FILE_PATH);

$sig = '47ccedfd3e857f83api_keyc6c3f471e99860ba35dae1dea6389faa';
$sig = $sig . 'auth_token' . $_POST['token'];

$sig = md5($sig);
$data['api_sig'] = $sig;

curl_setopt($cl, CURLOPT_POST, true);
curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($cl);
curl_close($cl);

//to delete the file
unlink($FILE_PATH);

?>